package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.dao.impl.logic.ResultSetParseEntityManger;
import com.yanqu.road.entity.player.UserPatronsContract;
import com.yanqu.road.entity.player.UserPatronsSkill;

import java.math.BigInteger;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserPatronsContractDaoImpl extends TempDataObjectDao<UserPatronsContract> {
	public static String getUserPatronsContractSql = "select * from `t_u_patrons_contract` where `userId`=?";
	public Map<Integer, Map<Integer, UserPatronsContract>> getUserPatronsContract(long userId) {
		Map<Integer, Map<Integer, UserPatronsContract>> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(getUserPatronsContractSql);
			int i = 1;
			call.setLong(i++, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserPatronsContract data = parse(rs);
				Map<Integer, UserPatronsContract> contractMap = dataMap.get(data.getPatronsId());
				if (contractMap == null) {
					contractMap = new ConcurrentHashMap<>();
					dataMap.put(data.getPatronsId(), contractMap);
				}
				contractMap.put(data.getPos(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public static String updateUserPatronsContractSql = "update `t_u_patrons_contract` set beautyId=?,abilityAdd=?,contractTime=? where userId=? and patronsId=? and pos=?";
	@Override
	protected boolean updateData(UserPatronsContract userPatronsContract) {
		userPatronsContract.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.INTEGER, userPatronsContract.getBeautyId()));
		para.put(i++, new DbParameter(Types.VARCHAR, userPatronsContract.getAbilityAdd().toString()));
		para.put(i++, new DbParameter(Types.BIGINT, userPatronsContract.getContractTime()));
		para.put(i++, new DbParameter(Types.BIGINT, userPatronsContract.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPatronsContract.getPatronsId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPatronsContract.getPos()));
		if (getDb().executeNoneQuery(updateUserPatronsContractSql, para) > 0) {
			return true;
		}
		return false;
	}

	public static String insertUserPatronsContractSql = "insert into `t_u_patrons_contract`(userId,patronsId,pos,beautyId,abilityAdd,contractTime) values (?,?,?,?,?,?)";
	@Override
	protected boolean addData(UserPatronsContract userPatronsContract) {
		userPatronsContract.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.BIGINT, userPatronsContract.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPatronsContract.getPatronsId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPatronsContract.getPos()));
		para.put(i++, new DbParameter(Types.INTEGER, userPatronsContract.getBeautyId()));
		para.put(i++, new DbParameter(Types.VARCHAR, userPatronsContract.getAbilityAdd().toString()));
		para.put(i++, new DbParameter(Types.BIGINT, userPatronsContract.getContractTime()));
		if (getDb().executeNoneQuery(insertUserPatronsContractSql, para) > 0) {
			return true;
		}
		return false;

	}

	@Override
	protected UserPatronsContract parseData(ResultSet rs) throws SQLException {
		UserPatronsContract userPatronsContract = new UserPatronsContract();
		userPatronsContract.setUserId(rs.getLong("userId"));
		userPatronsContract.setPatronsId(rs.getInt("patronsId"));
		userPatronsContract.setPos(rs.getInt("pos"));
		userPatronsContract.setBeautyId(rs.getInt("beautyId"));
		userPatronsContract.setAbilityAdd(new BigInteger(rs.getString("abilityAdd")));
		userPatronsContract.setContractTime(rs.getLong("contractTime"));

		return userPatronsContract;
	}

}
